package com.qf.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.attendance.entity.TimeSheet;
import com.qf.attendance.mapper.TimeSheetMapper;
import com.qf.attendance.qo.TimeSheetQO;
import com.qf.attendance.qo.Update;
import com.qf.attendance.service.TimeSheetService;
import com.qf.attendance.vo.ClocInVO;
import com.qf.attendance.vo.TimeSheetVO;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.utils.CommonBeanUtils;
import com.qf.common.db.utils.PageCommonBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class TimeSheetServiceImpl implements TimeSheetService {
    @Resource
    private TimeSheetMapper timeSheetMapper;

    /**
     * 分页查询考勤审核所有信息
     *
     * @param page 页码
     * @param size 条数
     * @return
     */
    @Override
    public IPage<TimeSheetVO> list(int page, int size) {
        return PageCommonBeanUtils.copyPage(timeSheetMapper.selectPage(new Page<>(page, size), null), new Page<>(), TimeSheetVO::new);
    }


    /**
     * 分页条件查询考勤信息
     *
     * @param page
     * @param size
     * @param timeSheetQO
     * @return
     */
    @Override
    public IPage<TimeSheetVO> selectList(int page, int size, TimeSheetQO timeSheetQO) {
        TimeSheet timeSheet = new TimeSheet();
        CommonBeanUtils.copyProperties(timeSheetQO, timeSheet);
        QueryWrapper<TimeSheet> wrapper = new QueryWrapper<>(timeSheet);
        return PageCommonBeanUtils.copyPage(timeSheetMapper.selectPage(new Page<>(page, size), wrapper), new Page<>(), TimeSheetVO::new);
    }

    /**
     * 添加考勤记录
     *
     * @param
     * @return
     */
    @Override
    public RespResult insert(TimeSheetQO timeSheetQO) {
        TimeSheet timeSheet = new TimeSheet();
        CommonBeanUtils.copyProperties(timeSheetQO, timeSheet);
        int count = timeSheetMapper.insert(timeSheet);
        if (count > 0) {
            return RespResult.success(null);
        }
        throw new RuntimeException("添加数据失败");
    }

    /**
     * 查询员工打卡记录
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<ClocInVO> clockIn(int page, int size) {
        return PageCommonBeanUtils.copyPage(timeSheetMapper.selectPage(new Page<>(page, size), null), new Page<>(), ClocInVO::new);
    }

    /**
     * 修改考勤考记录
     *
     * @param update
     * @return
     */
    @Override
    public RespResult update(Update update) {
        TimeSheet timeSheet = new TimeSheet();
        CommonBeanUtils.copyProperties(update, timeSheet);
        UpdateWrapper<TimeSheet> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("timeSheetId", update.getTimeSheetId());
        int count = timeSheetMapper.update(timeSheet, updateWrapper);
        if (count > 0) {
            return RespResult.success(null);
        }
        throw new RuntimeException("数据更新失败");
    }

    /**
     * 假删除
     *
     * @param timeSheetId
     * @return
     */
    @Override
    public RespResult delete(int timeSheetId) {
        int count = timeSheetMapper.deleteById(timeSheetId);
        if (count > 0) {
            return RespResult.success(null);
        }
        throw new RuntimeException("删除失败");
    }
}
